﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Slja.Model;
using Slja.DAL;
using AjaxPro.Services;
using System.Data;
using System.Text.RegularExpressions;


namespace Slja.BLL
{
    public class BLLCommon
    {
       [AjaxPro.AjaxMethod]
       public static string  GetCityFromKeyWord(string keyword)
        {
            string strXml = "";
            if (keyword.Length >= 2)
            {
                Regex regChz2 = new Regex(@"[\u0391-\uFFE5]{2,}");//2个以上的汉字
                Regex regEng2 = new Regex(@"[A-Z,a-z]{2,}");//2个以上的拼音字母

                string strWhere = "1=0";
                if (regChz2.IsMatch(keyword))
                {
                    strWhere = " f_title like '" + keyword + "%'";
                }
                if (regEng2.IsMatch(keyword))
                {
                    strWhere = " f_pyshort like '" + keyword + "%' OR f_pinyin like '"+keyword+"%'";
                }

                RegionCity dal = new RegionCity();
                DataSet ds = dal.GetList(strWhere);
                if (ds.Tables.Count > 0)
                {
                    strXml = BuildXmlData(ds.Tables[0], "fid", "f_title", "{0}*{1}", "{1}");
                }
            }
            return strXml;
        }     
     
       public static string BuildXmlData(DataTable dtSource, string valueField, string textField, string valueFmt, string textFmt)
       {
           StringBuilder sb = new StringBuilder();
           sb.Append("<?xml version='1.0' encoding='utf-8'?><all>");
           
            DataTable dt = dtSource;
            string na, id;
            na = ""; id = "";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                id = dt.Rows[i][valueField].ToString();
                na = dt.Rows[i][textField].ToString();
                sb.AppendFormat("<response value='" + valueFmt + "'>" + textFmt + "</response>", id, na);
            }
          
           sb.Append("</all>");
           return sb.ToString();
       }
       public static string ObjIDFromSpliting(string strIDName)
       {
           String[] arr = strIDName.Split('*');
           return arr[0];
       }
       public static string GetShopCode(int city, string ShopName,out string TLCode)
       {
           TLCode = "";
           return "";
       }
    }
}
